<template>
  <div class="goods-image">
    <div
      class="large"
      :style="[
        {
          backgroundImage: `url(${imageList[curIndex]})`,
          backgroundPositionX: positionX + 'px',
          backgroundPositionY: positionY + 'px',
        },
      ]"
      v-show="!isOutside"
    ></div>
    <div class="middle" ref="target">
      <img :src="imageList[curIndex]" alt="" />
      <!-- 蒙层容器 -->
      <div
        class="layer"
        :style="{ left: left + 'px', top: top + 'px' }"
        v-show="!isOutside"
      ></div>
    </div>
    <ul class="small">
      <li
        v-for="(img, i) in imageList"
        :key="i"
        @mouseenter="mouseEnter(i)"
        :class="{ active: i === curIndex }"
      >
        <img :src="img" alt="" />
      </li>
    </ul>
  </div>
</template>
<script>
// 交互思路
// 1.鼠标移入事件
// 2.鼠标移入哪个就把哪个的下标记录，然后通过下标取值，放到src渲染
import { ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    imageList: {
      type: Array,
      default: () => []
    }
  },
  setup () {
    // 实现鼠标移入
    const curIndex = ref(0)
    function mouseEnter (i) {
      curIndex.value = i
    }
    // 实现放大镜效果
    const target = ref(null)
    /**
     * elementX:盒子左侧距离
     * elementY：盒子顶部距离
     * isOutside:鼠标是否在盒子外部
     */
    const { elementX, elementY, isOutside } = useMouseInElement(target)
    // 监听x、y变化
    const left = ref(0)
    const top = ref(0)
    const positionX = ref(0)
    const positionY = ref(0)
    watch([elementX, elementY, isOutside], () => {
      // 只有进入容器才做判断
      // 根据鼠标坐标变化，控制left、top位置
      // 1.控制滑块最大可移动范围
      if (isOutside.value) {
        return false
      }
      if (elementX.value > 300) {
        left.value = 200
      }
      if (elementX.value < 100) {
        left.value = 0
      }
      // 2.横向有效移动范围
      if (elementX.value < 300 && elementX.value > 100) {
        left.value = elementX.value - 100
      }

      if (elementY.value > 300) {
        top.value = 200
      }
      if (elementY.value < 100) {
        top.value = 0
      }
      // 2.横向有效移动范围
      if (elementY.value < 300 && elementY.value > 100) {
        top.value = elementY.value - 100
      }
      // 控制背景大图移动
      // 1.鼠标方向相反
      // 2.鼠标每移动1个像素，大图移动2个像素
      positionX.value = -left.value * 2
      positionY.value = -top.value * 2
    })
    return {
      curIndex, mouseEnter, target, left, top, positionX, positionY, isOutside
    }
  }
}
</script>
<style scoped lang="less">
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
.large {
  position: absolute;
  top: 0;
  left: 412px;
  width: 400px;
  height: 400px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  background-repeat: no-repeat;
  background-size: 800px 800px;
  background-color: #f8f8f8;
}
.layer {
  width: 200px;
  height: 200px;
  background: rgba(0, 0, 0, 0.2);
  left: 0;
  top: 0;
  position: absolute;
}
</style>
